﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

using System.Drawing.Drawing2D;

using System.Diagnostics;



namespace FXnaGis
{

    /// <summary>
    /// 我们尝试着将线条等的样式，丰富化，并针对此问题建模
    /// </summary>
    [Serializable]
    public class LineStyle:Style
    {
        public LineStyle()
        {
            _color = StyleGlobal.RandomDarkColor(1);
            _width = 1;

            this._startCap = LineCap.Round;

            this._endCap = LineCap.Round;


        }


        //提供一个默认的通用样式

        public static LineStyle DefaultStyle = new LineStyle();

        #region Private Variables


        LineCap _startCap;

        public LineCap StartCap
        {
            get { return _startCap; }
            set { _startCap = value; }
        }

        LineCap _endCap;

        public LineCap EndCap
        {
            get { return _endCap; }
            set { _endCap = value; }
        }


        private Color _color;

        public Color Color
        {
            get { return _color; }
            set { _color = value; }
        }

        private DashStyle _dashStyle;

        public DashStyle DashStyle
        {
            get { return _dashStyle; }
            set { _dashStyle = value; }
        }


        private double _width;

        public double Width
        {
            get { return _width; }
            set { _width = value; }
        }


        public float Opacity
        {
            get { return _color.A / 255F; }
            set
            {
                float val = value;
                if (val > 1) val = 1F;
                if (val < 0) val = 0F;
                _color = Color.FromArgb((int)(val * 255), _color.R, Color.G, Color.B);
            }
        }


        #endregion


        /// <summary>
        /// This is an optional expression that allows drawing to the specified GraphicsPath.
        /// Overriding this allows for unconventional behavior to be included, such as
        /// specifying marker decorations, rather than simply returning a pen.  A pen
        /// is also returned publicly for convenience.
        /// </summary>
        /// <param name="g">The Graphics device to draw to</param>
        /// <param name="path">the GraphicsPath to draw</param>
        /// <param name="scaleWidth">This is 1 for symbolic drawing, but could be
        /// any number for geographic drawing.</param>
        public virtual void DrawPath(Graphics g, GraphicsPath path, double scaleWidth)
        {
            //if (_innerStroke != null)
            //{
            //    _innerStroke.DrawPath(g, path, scaleWidth);
            //    return;
            //}

            Pen p = ToPen(scaleWidth);
            try
            {
                g.DrawPath(p, path);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Writeline Stroke.DrawPath:" + ex);
            }
            p.Dispose();
        }

        /// <summary>
        /// Uses the properties specified in this class to generate a pen.
        /// </summary>
        /// <returns></returns>
        public virtual Pen ToPen(double width)
        {
            float w = (float)(width * Width);
            Pen result = new Pen(Color, w);
            result.DashStyle = _dashStyle;
            if (_dashStyle == DashStyle.Custom)
            {
                result.DashPattern = new[] { 1F };
            }
            result.LineJoin = LineJoin.Round;
            result.StartCap = this._startCap;
            result.EndCap = this._endCap;
            return result;

        }


      




    }
}
